package com.example.recordingpen_datacollection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.recordingpen_datacollection.entity.Vo.LimitDosageVo;
import com.example.recordingpen_datacollection.entity.Vo.VoiceUse;
import com.example.recordingpen_datacollection.entity.VoiceLogin;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.VoiceConversionService;
import com.example.recordingpen_datacollection.service.VoiceLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-08-07
 */
@Api(tags = "语音转换控制")
@RestController
@RequestMapping("/voice-conversion")
public class VoiceConversionController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private VoiceLoginService voiceLoginService;
    @Autowired
    private VoiceConversionService voiceConversionService;

    @ApiOperation("语音使用")
    @PostMapping("/voiceUse")
    public RLIST voiceUse(@RequestBody VoiceUse voiceUse, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        Integer  value = (Integer) redisService.getValue(token);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("log_id",value);
        VoiceLogin byId = voiceLoginService.getOne(wrapper);
        boolean flat = voiceConversionService.setVoiceUse(byId,voiceUse);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error().message("超出用量");
        }
    }


    @ApiOperation("查询语音使用")
    @PostMapping("/getVoiceUse")
    public RLIST getVoiceUse(HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 36 ){
            return RLIST.error().message("无权操作");
        }
        Integer  value = (Integer) redisService.getValue(token);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("log_id",value);
        VoiceLogin byId = voiceLoginService.getOne(wrapper);

        Integer  dosage = voiceConversionService.getVoiceUse(value);
        Map<String,Integer> map = new HashMap<>();
        map.put("dosage",dosage);
        map.put("vlSumdosage",byId.getVlSumdosage());
        return RLIST.ok().data("data",map);
    }

}

